//package com.hc.testaudio.delete.util.start
//
//import android.graphics.*
//import android.media.AudioRecord
//import io.reactivex.Observable
//import io.reactivex.disposables.Disposable
//import io.reactivex.schedulers.Schedulers
//import android.view.SurfaceView
//
//
///**
// *
// */
//class AudioHelper {
//    private var mInputBuffer = ArrayList<ShortArray>()
//    private var mOutBuffer = ArrayList<IntArray>()
//    private var mIsRecording = false //是否在录音
//    private var mDisposableRecord: Disposable? = null
//    private var mLength = 256
//    //y轴缩小的比例
//    var rateY = 21
//    var frequence = 0
//    //y轴基线
//    public var baseLine = 0f
//
//    fun startRecord(audioRecord: AudioRecord, minBufferSize: Int) {
//        mIsRecording = true
//        mDisposableRecord?.dispose()
//        mDisposableRecord = Observable.just(1).subscribeOn(Schedulers.newThread()) //开启新线程
//                .subscribe {
//                    val buffer = ShortArray(minBufferSize)
//                    audioRecord.startRecording()
//                    while (mIsRecording) {
//                        val res = audioRecord.read(buffer, 0, minBufferSize)
//                        synchronized(mInputBuffer) {
//                            mInputBuffer.add(buffer)
//                        }
//                        mLength = up2int(res)
//                        val tmpBuf = ShortArray(mLength)
//                        System.arraycopy(buffer, 0, tmpBuf, 0, mLength)
//
//                        val complexList: ArrayList<Complex> = ArrayList<Complex>()
//
//                        val outInt = IntArray(mLength)
//                        for (i in 0 until mLength) {
//                            complexList.add(Complex(tmpBuf[i].toDouble()))
//                        }
//                        val fftData = Temp.fft(complexList, mLength)
//                        for (i in 0 until mLength) {
//                            outInt[i] = fftData[i].intAbs
//                        }
//                        synchronized(mOutBuffer) {
//                            mOutBuffer.add(outInt)
//                        }
//                    }
//                    audioRecord.stop() //关闭录音
//                }
//    }
//
//    /**
//     * 向上取最接近data的2的幂次数.比如data=320时,返回256
//     */
//    private fun up2int(data: Int): Int {
//        var ret = 1
//        while (ret <= data) {
//            ret = ret shl 1
//        }
//        return ret shr 1
//    }
//
//    fun stop() {
//        mIsRecording = false
//        mInputBuffer.clear()
//    }
//
//    fun start(audioRecord: AudioRecord, minBufferSize: Int, sfvSurfaceView: SurfaceView) {
//        startRecord(audioRecord, minBufferSize)
//        DrawThread(sfvSurfaceView).start()
//    }
//
//    //绘图线程
//    internal inner class DrawThread(private val sfvSurfaceView: SurfaceView) : Thread() {
//
//        //当前画图所在屏幕x轴的坐标
//        //画笔
//        private val mPaint: Paint
//        private val tPaint: Paint
//        private val dashPaint: Paint
//
//        init {
//            //设置画笔属性
//            mPaint = Paint()
//            mPaint.setColor(Color.BLUE)
//            mPaint.setStrokeWidth(2f)
//            mPaint.setAntiAlias(true)
//
//            tPaint = Paint()
//            tPaint.setColor(Color.YELLOW)
//            tPaint.setStrokeWidth(1f)
//            tPaint.setAntiAlias(true)
//
//            //画虚线
//            dashPaint = Paint()
//            dashPaint.setStyle(Paint.Style.STROKE)
//            dashPaint.setColor(Color.GRAY)
//            val path = Path()
//            path.moveTo(0f, 10f)
//            path.lineTo(480f, 10f)
//            val effects = DashPathEffect(floatArrayOf(5f, 5f, 5f, 5f), 1f)
//            dashPaint.setPathEffect(effects)
//        }
//
//        override fun run() {
//            while (mIsRecording) {
//                val buf = ArrayList<IntArray>()
//                synchronized(mOutBuffer) {
//                    if (mOutBuffer.size != 0) {
//                        buf.addAll(mOutBuffer)
//                        mOutBuffer.clear()
//                    }
//                }
//                //根据ArrayList中的short数组开始绘图
//                for (i in 0 until buf.size) {
//                    val tmpBuf = buf[i]
//                    SimpleDraw(tmpBuf, rateY, baseLine)
//                }
//            }
//        }
//
//
//        /**
//         * 绘制指定区域
//         *
//         * @param start
//         * X 轴开始的位置(全屏)
//         * @param buffer
//         * 缓冲区
//         * @param rate
//         * Y 轴数据缩小的比例
//         * @param baseLine
//         * Y 轴基线
//         */
//
//        private fun SimpleDraw(buffer: IntArray, rate: Int, baseLine: Float) {
//            val shift = 30f
//            val canvas = sfvSurfaceView.holder.lockCanvas(Rect(0, 0, buffer.size, sfvSurfaceView.height))
//            canvas.drawColor(Color.BLACK)
//            canvas.drawText("幅度值", 0, 3, 2f, 15f, tPaint)
//            canvas.drawText("原点(0,0)", 0, 7, 5f, baseLine + 15f, tPaint)
//            canvas.drawText("频率(HZ)", 0, 6, sfvSurfaceView.width - 50f, baseLine + 30f, tPaint)
//            canvas.drawLine(shift, 20f, shift, baseLine, tPaint)
//            canvas.drawLine(shift, baseLine, sfvSurfaceView.width.toFloat(), baseLine, tPaint)
//            canvas.save()
//            canvas.rotate(30f, shift, 20f)
//            canvas.drawLine(shift, 20f, shift, 30f, tPaint)
//            canvas.rotate(-60f, shift, 20f)
//            canvas.drawLine(shift, 20f, shift, 30f, tPaint)
//            canvas.rotate(30f, shift, 20f)
//            canvas.rotate(30f, sfvSurfaceView.width - 1f, baseLine)
//            canvas.drawLine(sfvSurfaceView.width - 1f, baseLine, sfvSurfaceView.width - 11f, baseLine, tPaint)
//            canvas.rotate(-60f, sfvSurfaceView.width - 1f, baseLine)
//            canvas.drawLine(sfvSurfaceView.width - 1f, baseLine, sfvSurfaceView.width - 11f, baseLine, tPaint)
//            canvas.restore()
//            //tPaint.setStyle(Style.STROKE);
//            var index = 64
//            while (index <= 512) {
//                canvas.drawLine(shift + index, baseLine, shift + index, 40f, dashPaint)
//                val str = (frequence / 1024 * index).toString()
//                canvas.drawText(str, 0, str.length, shift + index - 15, baseLine + 15, tPaint)
//                index += 64
//            }
//            var y: Int
//            var i = 0
//            while (i < buffer.size) {
//                y = (baseLine - buffer[i] / rateY).toInt()
//                canvas.drawLine(2 * i + shift, baseLine, 2 * i + shift, y.toFloat(), mPaint)
//                i += 1
//            }
//            sfvSurfaceView.holder.unlockCanvasAndPost(canvas)
//        }
//    }
//}